//
//  HomeStatisticsCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/6/22.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "HomeStatisticsCell.h"
#import <WebKit/WebKit.h>
//#import "HomeStatistics3PLVC.h"
//#import "HomeStatisticsCarVC.h"
//#import "HomeStatisticsTranBillVC.h"
//#import "HomeStatisticsTranMileageVC.h"

@interface HomeStatisticsCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIButton *gifBtn;
@property (nonatomic, strong) HomeStatisticsCellBtnView *view1;
@property (nonatomic, strong) HomeStatisticsCellBtnView *view2;
@property (nonatomic, strong) HomeStatisticsCellBtnView *view3;
@property (nonatomic, strong) HomeStatisticsCellBtnView *view4;

@end

@implementation HomeStatisticsCell

+ (instancetype)HomeStatisticsCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HomeStatisticsCell";
    HomeStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomeStatisticsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        CGFloat gifW = ScreenW - 20 - 16;
        CGFloat gifH = gifW * 200.0 / 690.0;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 40 + gifH + 40)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        NSData *gif = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"home_qiangDan.gif" ofType:nil]];
        self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(8, 40, gifW, gifH)];
        self.webView.userInteractionEnabled = NO;
        [self.webView loadData:gif MIMEType:@"image/gif" characterEncodingName:nil baseURL:nil];
        [self.bgView addSubview:self.webView];
        
        self.gifBtn = [[UIButton alloc] initWithFrame:CGRectMake(8, 40, gifW, gifH)];
        [self.gifBtn addTarget:self action:@selector(gifClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.gifBtn];
        
        self.view1 = [HomeStatisticsCellBtnView new];
        self.view1.clickBlock = ^{
//            HomeStatistics3PLVC *vc = [HomeStatistics3PLVC new];
//            vc.hidesBottomBarWhenPushed = YES;
//            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view1];
        
        self.view2 = [HomeStatisticsCellBtnView new];
        self.view2.clickBlock = ^{
//            HomeStatisticsTranBillVC *vc = [HomeStatisticsTranBillVC new];
//            vc.hidesBottomBarWhenPushed = YES;
//            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view2];
        
        self.view3 = [HomeStatisticsCellBtnView new];
        self.view3.clickBlock = ^{
//            HomeStatisticsCarVC *vc = [HomeStatisticsCarVC new];
//            vc.hidesBottomBarWhenPushed = YES;
//            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view3];
        
        self.view4 = [HomeStatisticsCellBtnView new];
        self.view4.clickBlock = ^{
//            HomeStatisticsTranMileageVC *vc = [HomeStatisticsTranMileageVC new];
//            vc.hidesBottomBarWhenPushed = YES;
//            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view4];
        
        self.height = self.bgView.nim_bottom;
    }
    return self;
}

- (void)gifClick {
    [[FactoryMethod getCurrentVC].rt_navigationController popToRootViewControllerAnimated:NO];
    MainTabBarController *vc = [MainTabBarController instance];
    vc.idx = 1;
    [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshGrabOrderList object:nil];
}

-(void)setDataArray:(NSMutableArray<HomeStatisticsModel *> *)dataArray {
    _dataArray = dataArray;
    [dataArray enumerateObjectsUsingBlock:^(HomeStatisticsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat view1W = [FactoryMethod sizeWithText:obj.title font:BoldFontOfSize(14) maxSize:CGSizeMake(0, 0)].width;
        CGFloat view1H = [FactoryMethod sizeWithText:obj.title font:BoldFontOfSize(14) maxSize:CGSizeMake(0, 0)].height + [FactoryMethod sizeWithText:obj.content font:BoldFontOfSize(12) maxSize:CGSizeMake(0, 0)].height + 3;
        if (idx == 0) {
            self.view1.frame = CGRectMake(8, 40 - view1H, view1W, view1H);
            self.view1.model = obj;
        }else if (idx == 1) {
            self.view2.frame = CGRectMake(self.bgView.nim_width - 8 - view1W, 40 - view1H, view1W, view1H);
            self.view2.model = obj;
        }else if (idx == 2) {
            self.view3.frame = CGRectMake(8, self.webView.nim_bottom, view1W, view1H);
            self.view3.model = obj;
        }else if (idx == 3) {
            self.view4.frame = CGRectMake(self.bgView.nim_width - 8 - view1W, self.webView.nim_bottom, view1W, view1H);
            self.view4.model = obj;
        }
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface HomeStatisticsCellBtnView ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UIButton *btn;

@end

@implementation HomeStatisticsCellBtnView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLab = [UILabel new];
        self.titleLab.textColor = HexColor(0x333333);
        self.titleLab.font = BoldFontOfSize(14);
        [self addSubview:self.titleLab];
        
        self.contentLab = [UILabel new];
        self.contentLab.textColor = Color_ThemeColor;
        self.contentLab.font = BoldFontOfSize(12);
        [self addSubview:self.contentLab];
        
        self.btn = [UIButton new];
        [self.btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btn];
    }
    return self;
}

- (void)btnClick {
    if (self.clickBlock) {
        self.clickBlock();
    }
}

-(void)setModel:(HomeStatisticsModel *)model {
    _model = model;
    self.titleLab.text = model.title;
    self.titleLab.nim_left = 0;
    self.titleLab.nim_top = 0;
    [self.titleLab sizeToFit];
    
    self.contentLab.text = model.content;
    [self.contentLab sizeToFit];
    self.contentLab.nim_centerX = self.titleLab.nim_centerX;
    self.contentLab.nim_top = self.titleLab.nim_bottom + 3;
    
    self.btn.frame = CGRectMake(0, 0, self.titleLab.nim_width, self.titleLab.nim_height + self.contentLab.nim_height + 3);
}

@end
